Questions
19 of 38
1What is the Iterator Protocol in JavaScript? What two things must an object implement to be a valid iterator?
2What is the difference between an iterable and an iterator? Can something be both?
3What built-in JavaScript data structures are iterable by default? How does for...of work under the hood with them?
4What does Symbol.iterator do? How would you make a plain object iterable from scratch?
5What is a 'lazy' iterator and why is it important for performance? Give a practical example.
6How would you implement an infinite iterator (e.g., an infinite counter)? How do you safely consume it?
7What happens if you forget to return { done: true } in a custom iterator? What are the consequences?
8How would you implement a reusable iterable (one that can be iterated multiple times)?
9Can you chain or compose custom iterators? Implement a map and filter for a custom iterator without converting to an array.
10What is a generator function? How is it different from a regular function in terms of execution flow?
11Explain what yield does. What does calling .next() return before and after a yield?
12What is the difference between yield and return inside a generator?
13What does yield* do? How is it different from a regular yield?
14Can you pass a value into a generator using .next(value)? How does that work and what is the practical use case?
15Explain generator.return(value) and generator.throw(error). When would you use each in production?
16What happens to a try/finally block inside a generator when .return() is called externally?
17How do generators handle errors? How does .throw() interact with try/catch inside a generator?
18Are generators lazy? How do they differ from eager evaluation with arrays in terms of memory and performance?
19What is the difference between a regular iterator and an async iterator? What protocol does an async iterator follow?
20What does Symbol.asyncIterator do? How does for await...of use it?
21Implement an async generator that fetches paginated API data, yielding one page at a time:
22What are the pitfalls of using for await...of with an async generator that makes network calls? How do you handle errors and cancellation?
23How would you implement a readable stream as an async iterable in Node.js?
24How would you use a generator to implement redux-saga-style side effect management? What makes generators a good fit for this?
25How can generators be used to implement coroutines or cooperative multitasking in JavaScript?
26Implement a take(n) utility that takes the first n values from any iterable — including infinite ones:
27When would you choose a generator over returning an array? What are the memory trade-offs?
28In a data pipeline processing millions of records, how would you use generators to avoid loading everything into memory?
29What are the debugging challenges with generators (e.g., in stack traces and async flows)? How do you mitigate them?
30How do generators compose? Implement a pipeline of generator-based transformations (like RxJS operators but synchronous).
31What are the limitations of generators? What problems are they not a good fit for?
32What are Iterator Helpers (.map(), .filter(), .take(), .drop() on iterators natively)? What stage are they at in the TC39 proposal pipeline?
33How does Array.from() use the iterator protocol? What's the difference between passing an iterable vs an array-like object?
34How do Map, Set, Array, and String expose their iterators? Are they the same object or different?
35What is the difference between map.keys(), map.values(), and map.entries()? What do they return?
36How does destructuring and spread (...) use the iterator protocol internally?
37How would you implement Promise-based async/await using generators and a runner function? (This is essentially how Babel transpiled async/await early on.)
38How would you use a generator to implement a tree traversal (DFS) without recursion stack concerns?
19 / 38

What is the difference between a regular iterator and an async iterator? What protocol does an async iterator follow?

An async iterator returns Promises from next(), following the Async Iterator Protocol with Symbol.asyncIterator. Values are resolved asynchronously.

Regular iterators return { value, done } directly. Async iterators return a Promise that resolves to { value, done }, allowing iteration over asynchronous data sources (e.g., streams, paginated APIs). They are consumed with for await...of loops.

Difficulty: 5/10
Topics: iterator protocol, async iteration, Symbol.asyncIterator

Scenario Questions

0-2 years experience
  1. 1

    We have a function that returns an array of user IDs. How would you loop over it with a regular iterator, and what would you need to change to handle a stream of IDs that arrives asynchronously?

  2. 2

    If you write for await (const x of someIterable) on an object that only implements Symbol.iterator, what runtime error do you see and why?

  3. 3

    Show me a small Node.js snippet that reads a file line‑by‑line using an async iterator so the whole file isn’t loaded into memory.

2-5 years experience
  1. 1

    Our service fetches paginated data from an external API returning a promise per page. We tried a for...of loop and got unexpected behavior. Walk me through why that happened and how you’d fix it with async iteration.

  2. 2

    During a code review a teammate swapped a sync iterator for an async one but forgot to implement Symbol.asyncIterator. The code compiled but failed at runtime. How would you detect and debug this issue?

  3. 3

    We have a utility that accepts any iterable and returns an array of its values. How would you extend it to also support async iterables without breaking existing callers?

5-8 years experience
  1. 1

    Our data pipeline processes millions of records from a message queue using a sync iterator over a buffered array, causing back‑pressure problems. Design a solution using async iterators to provide proper flow control and discuss trade‑offs.

  2. 2

    We need to stream large JSON results to clients over HTTP/2. Explain how you’d implement the server‑side using async iterators, and what you must consider for cancellation and error propagation.

  3. 3

    A legacy module uses custom iterator objects exposing a next() method but not Symbol.iterator. We want to migrate to async iteration across the codebase. Outline a migration strategy that minimizes risk and keeps compatibility.

8+ years experience
  1. 1

    Our organization is standardizing on async iteration for all I/O streams. What architectural guidelines would you set for libraries, testing, and documentation to ensure consistent use of the async iterator protocol across multiple teams?

  2. 2

    We have a monorepo with both React front‑ends and Node.js services. Some shared utilities need to work in both sync and async contexts. How would you design an abstraction that can be consumed as either a regular iterator or an async iterator, and what impact does that have on type definitions and build pipelines?

  3. 3

    Considering possible future changes to the async iterator protocol, how would you future‑proof our codebase to handle protocol extensions without breaking existing consumers?

Follow-up Questions

  • What does Symbol.asyncIterator represent on an object?
  • How does error handling differ between sync and async iterators?
  • Can you describe how back‑pressure is managed with async iteration?